﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using CMISLibrary.Libraries;
using CMISLibrary;
using System.Data;
using System.Threading;
using BaocaoNPC.Usercontrols.reports;
using BaocaoNPC.common;
using bus_TienichNPC;

namespace CMIS2.BaocaoNPC.Usercontrols
{
    /// <summary>
    /// Interaction logic for TRUYTHU_BAOCAO.xaml
    /// </summary>
    public partial class TRUYTHU_BAOCAO : UserControl
    {

        #region KhaiBaoBien
        /*
        public String strMa_dviqly = UserInfomation.MaDViqly != "" && UserInfomation.MaDViqly != null ? UserInfomation.MaDViqly : "PA";
        public String strTenDNHap = UserInfomation.TenDNHap != "" && UserInfomation.TenDNHap != null ? UserInfomation.TenDNHap : "PA";
        public String strCurrentLibID = MenuInfomation.LibID != "" && MenuInfomation.LibID != null ? MenuInfomation.LibID : "65";
        public String strTen_dviqly = UserInfomation.TenDViQLy != "" && UserInfomation.TenDViQLy != null ? UserInfomation.TenDViQLy : "cty..";
        public String strTen_dviqly_CT = UserInfomation.TenDViQLy_CT != "" && UserInfomation.TenDViQLy_CT != null ? UserInfomation.TenDViQLy_CT : "Cong ty Dien luc...";
         * */
        public String strMa_dviqly = NPCUS_DanhMuc.Pglobal.strMa_dviqly != "" && NPCUS_DanhMuc.Pglobal.strMa_dviqly != null ? NPCUS_DanhMuc.Pglobal.strMa_dviqly : "PA";
        public String strTenDNHap = NPCUS_DanhMuc.Pglobal.strTen_dangnhap != "" && NPCUS_DanhMuc.Pglobal.strTen_dangnhap != null ? NPCUS_DanhMuc.Pglobal.strTen_dangnhap : "PA";
        public String strTen_dviqly = NPCUS_DanhMuc.Pglobal.strTen_dviqly != "" && NPCUS_DanhMuc.Pglobal.strTen_dviqly != null ? NPCUS_DanhMuc.Pglobal.strTen_dviqly : "cty..";
        public String strTen_dviqly_CT = NPCUS_DanhMuc.Pglobal.strTen_dviqly_CT != "" && NPCUS_DanhMuc.Pglobal.strTen_dviqly_CT != null ? NPCUS_DanhMuc.Pglobal.strTen_dviqly_CT : "cap tren";

        public Int32 i16ThangBC = 0;
        public Int32 i16NamBC = 0;

        public DateTime dtTungay;
        public DateTime dtDenngay;

        string strLoaiBC = "";
        string strLoaiDate = "";
        
        public string strMa_dviqly_InBC = "";
        public string strTen_dviqly_InBC = "";
        public string strTen_dviqly_CT_InBC = "";
        cls_TienichNPC_bus cls = new cls_TienichNPC_bus();
        

        #endregion


        public TRUYTHU_BAOCAO()
        {
            InitializeComponent();
            dtpThang.SelectedDate = DateTime.Now;
            dtpNam.SelectedDate = DateTime.Now;
            dtpTungay.Text = "";
            dtpDenngay.Text = "";

            rdbBieu1.IsChecked = true;
            strLoaiBC = "BIEU1";
            optThang.IsChecked = true;
            strLoaiDate = "THANG";
            //dtpThang.IsEnabled = true;
            //dtpNam.IsEnabled = false;
            //dtpTungay.IsEnabled = false;
            //dtpDenngay.IsEnabled = false;

            GetD_DVI_QLY();
        }

        private void GetD_DVI_QLY()
        {
            try
            {
                DataSet dsD_DVI_QLY = new DataSet();                

                dsD_DVI_QLY = GlobalServices.Service_NPCUS.select_D_DVI_QLY_ALL(strMa_dviqly);

                if (dsD_DVI_QLY == null)
                {
                    System.Windows.MessageBox.Show("Không lấy được danh mục đơn vị quản lý.");
                }
                else
                {
                    DataView dv = dsD_DVI_QLY.Tables["D_DVI_QLY"].DefaultView;
                    cboDonViQuanLy.ItemsSource = dv;
                    cboDonViQuanLy.DisplayMemberPath = "TEN_DVIQLY";
                    cboDonViQuanLy.SelectedValuePath = "MA_DVIQLY";
                    cboDonViQuanLy.SelectedIndex = 0;                    
                }
            }
            catch (Exception ex)
            {
                System.Windows.MessageBox.Show(ex.Message.ToString());
            }
        }

        private void GET_THANGNAM_MADVIQLY_INBC()
        {
            try
            {                
                strMa_dviqly_InBC = ((DataRowView)cboDonViQuanLy.SelectedItem)["MA_DVIQLY"].ToString();
                strTen_dviqly_InBC = ((DataRowView)cboDonViQuanLy.SelectedItem)["TEN_DVIQLY"].ToString();
                strTen_dviqly_CT_InBC = strTen_dviqly_CT;
            }
            catch
            {                
                strMa_dviqly_InBC = strMa_dviqly;
                strTen_dviqly_InBC = strTen_dviqly;
                strTen_dviqly_CT_InBC = strTen_dviqly_CT;
            }
        }

        private void rdbBieu1_Checked(object sender, RoutedEventArgs e)
        {
            strLoaiBC = "BIEU1";
        }

        private void rdbBieu2_Checked(object sender, RoutedEventArgs e)
        {
            strLoaiBC = "BIEU2";
        }

        private void optThang_Checked(object sender, RoutedEventArgs e)
        {
            //dtpThang.IsEnabled = true;
            //dtpNam.IsEnabled = false;
            //dtpTungay.IsEnabled = false;
            //dtpDenngay.IsEnabled = false;
            strLoaiDate = "THANG";
        }

        private void optNam_Checked(object sender, RoutedEventArgs e)
        {
            //dtpThang.IsEnabled = false;
            //dtpNam.IsEnabled = true;
            //dtpTungay.IsEnabled = false;
            //dtpDenngay.IsEnabled = false;
            strLoaiDate = "NAM";
        }

        private void optNgay_Checked(object sender, RoutedEventArgs e)
        {
            //dtpThang.IsEnabled = false;
            //dtpNam.IsEnabled = false;
            //dtpTungay.IsEnabled = true;
            //dtpDenngay.IsEnabled = true;
            strLoaiDate = "KHOANGTG";
        }

        private void btnInbaocao_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                GET_THANGNAM_MADVIQLY_INBC();                
                DataSet ds = new DataSet();
                if (strLoaiDate == "THANG")
                {
                    ds = GlobalServices.Service_Truythu.BaoCao_Truythu(strMa_dviqly_InBC, strLoaiBC, strLoaiDate, dtpThang.SelectedDate.HasValue ? dtpThang.SelectedDate.Value.Month : 0, dtpThang.SelectedDate.HasValue ? dtpThang.SelectedDate.Value.Year : 0, dtpTungay.SelectedDate.HasValue ? dtpTungay.SelectedDate.Value.Date : Convert.ToDateTime("01/01/1000"), dtpDenngay.SelectedDate.HasValue ? dtpDenngay.SelectedDate.Value.Date : Convert.ToDateTime("01/01/1000"));
                }
                else
                {
                    ds = GlobalServices.Service_Truythu.BaoCao_Truythu(strMa_dviqly_InBC, strLoaiBC, strLoaiDate, dtpThang.SelectedDate.HasValue ? dtpThang.SelectedDate.Value.Month : 0, dtpNam.SelectedDate.HasValue ? dtpNam.SelectedDate.Value.Year : 0, dtpTungay.SelectedDate.HasValue ? dtpTungay.SelectedDate.Value.Date : Convert.ToDateTime("01/01/1000"), dtpDenngay.SelectedDate.HasValue ? dtpDenngay.SelectedDate.Value.Date : Convert.ToDateTime("01/01/1000"));
                }

                if (ds != null && ds.Tables[0].Rows.Count > 0)
                {
                    if (strLoaiBC == "BIEU1")
                    {
                        rpt_NPC_TRTH_BC_BIEU1 rpt = new rpt_NPC_TRTH_BC_BIEU1();
                        rpt.SetDataSource(ds.Tables[0]);

                        rpt.SetParameterValue("TEN_CTREN", strTen_dviqly_CT_InBC.ToUpper());
                        rpt.SetParameterValue("TEN_DVIQLY", strTen_dviqly_InBC);

                        if (strLoaiDate == "NAM")
                        {
                            rpt.SetParameterValue("tgian", "Năm " + dtpNam.SelectedDate.Value.Year.ToString());
                        }
                        else
                            if (strLoaiDate == "THANG")
                            {
                                rpt.SetParameterValue("tgian", "Tháng " + dtpThang.SelectedDate.Value.Month.ToString() + " năm " + dtpThang.SelectedDate.Value.Year.ToString());
                            }
                            else
                                if (strLoaiDate == "KHOANGTG")
                                {
                                    rpt.SetParameterValue("tgian", "Từ ngày " + dtpTungay.SelectedDate.Value.Date.ToString("dd/MM/yyyy") + " đến ngày " + dtpDenngay.SelectedDate.Value.Date.ToString("dd/MM/yyyy"));
                                }
                        UtilReport.RptManager.showReportToWin(this, rpt, strMa_dviqly, new string[] { "Formula1", "Formula2" }, new string[] { "Formula1", "Formula2" });
                    }
                    else
                        if (strLoaiBC == "BIEU2")
                        {
                            rpt_NPC_TRTH_BC_BIEU2 rpt = new rpt_NPC_TRTH_BC_BIEU2();
                            rpt.SetDataSource(ds.Tables[0]);

                            rpt.SetParameterValue("TEN_CTREN", strTen_dviqly_CT_InBC.ToUpper());
                            rpt.SetParameterValue("TEN_DVIQLY", strTen_dviqly_InBC);

                            if (strLoaiDate == "NAM")
                            {
                                rpt.SetParameterValue("tgian", "Năm " + dtpNam.SelectedDate.Value.Year.ToString());
                            }
                            else
                                if (strLoaiDate == "THANG")
                                {
                                    rpt.SetParameterValue("tgian", "Tháng " + dtpThang.SelectedDate.Value.Month.ToString() + " năm " + dtpThang.SelectedDate.Value.Year.ToString());
                                }
                                else
                                    if (strLoaiDate == "KHOANGTG")
                                    {
                                        rpt.SetParameterValue("tgian", "Từ ngày " + dtpTungay.SelectedDate.Value.Date.ToString("dd/MM/yyyy") + " đến ngày " + dtpDenngay.SelectedDate.Value.Date.ToString("dd/MM/yyyy"));
                                    }
                            UtilReport.RptManager.showReportToWin(this, rpt, strMa_dviqly, new string[] { "Formula1", "Formula2" }, new string[] { "Formula1", "Formula2" });

                            //ds.WriteXmlSchema("C:\\TRTH_BIEU2.xsd");
                        }
                        else
                            if (strLoaiBC == "BIEU3")
                            {
                                rpt_NPC_TRTH_BC_BIEU3 rpt = new rpt_NPC_TRTH_BC_BIEU3();
                                rpt.SetDataSource(ds.Tables[0]);

                                rpt.SetParameterValue("TEN_CTREN", strTen_dviqly_CT_InBC.ToUpper());
                                rpt.SetParameterValue("TEN_DVIQLY", strTen_dviqly_InBC);

                                if (strLoaiDate == "NAM")
                                {
                                    rpt.SetParameterValue("tgian", "Năm " + dtpNam.SelectedDate.Value.Year.ToString());
                                }
                                else
                                    if (strLoaiDate == "THANG")
                                    {
                                        rpt.SetParameterValue("tgian", "Tháng " + dtpThang.SelectedDate.Value.Month.ToString() + " năm " + dtpThang.SelectedDate.Value.Year.ToString());
                                    }
                                    else
                                        if (strLoaiDate == "KHOANGTG")
                                        {
                                            rpt.SetParameterValue("tgian", "Từ ngày " + dtpTungay.SelectedDate.Value.Date.ToString("dd/MM/yyyy") + " đến ngày " + dtpDenngay.SelectedDate.Value.Date.ToString("dd/MM/yyyy"));
                                        }
                                UtilReport.RptManager.showReportToWin(this, rpt, strMa_dviqly, new string[] { "Formula1", "Formula2" }, new string[] { "Formula1", "Formula2" });

                                //ds.WriteXmlSchema("C:\\TRTH_BIEU3.xsd");
                            }




                }
                else
                {
                    MessageBox.Show("Không có dữ liệu !");
                }
            }
            catch(Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }
        }

        private void rdbBieu3_Checked(object sender, RoutedEventArgs e)
        {
            strLoaiBC = "BIEU3";
        }
    }
}
